//
//  LoginViewController.m
//  UI-21-豆豆豆瓣
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 Wang Hao. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "DatabaseSingleton.h"
#import "Users.h"

@interface LoginViewController ()

@property (nonatomic, retain) UILabel *aLabel;
@property (nonatomic, retain) UILabel *bLabel;
@property (nonatomic, retain) UITextField *aField;
@property (nonatomic, retain) UITextField *bField;
@property (nonatomic, retain) UIButton *button1;
@property (nonatomic, retain) UIButton *button2;

@end

@implementation LoginViewController

- (void)dealloc
{
    
    [_aField release];
    [_aLabel release];
    [_bField release];
    [_bLabel release];
    [_button1 release];
    [_button2 release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"用户登录";
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftActio:)] autorelease];
    
    
    
    self.aLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 90, 100, 40)];
    self.aLabel.text = @"用户名 :";
    [self.view addSubview:self.aLabel];
    [_aLabel release];
    
    self.bLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 150, 100, 40)];
    self.bLabel.text = @"密码 :";
    [self.view addSubview:self.bLabel];
    [_bLabel release];
    
    self.aField = [[UITextField alloc] initWithFrame:CGRectMake(140, 100, 150, 30)];
    self.aField.borderStyle = UITextBorderStyleRoundedRect;
    self.aField.placeholder = @"请输入用户名";
    [self.view addSubview:self.aField];
    [_aField release];
    
    self.bField = [[UITextField alloc] initWithFrame:CGRectMake(140, 160, 150, 30)];
    self.bField.borderStyle = UITextBorderStyleRoundedRect;
    self.bField.placeholder = @"请输入密码";
    self.bField.secureTextEntry = YES;
    [self.view addSubview:self.bField];
    [_bField release];
    self.button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.button1 setBackgroundImage:[UIImage imageNamed:@"111"] forState:UIControlStateNormal];
    self.button1.frame = CGRectMake(40, 250, 120, 45);
    [self.view addSubview:self.button1];
    [self.button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
    
    self.button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.button2.frame = CGRectMake(220, 250, 120, 45);
    [self.button2 setBackgroundImage:[UIImage imageNamed:@"222"] forState:UIControlStateNormal];
    [self.view addSubview:self.button2];
    [self.button2 addTarget:self action:@selector(button2Action :) forControlEvents:UIControlEventTouchUpInside];
    

}
-(void)button1Action:(UIButton *)button{
    [self.aField resignFirstResponder];
    [self.bField resignFirstResponder];
    //判断登录
    Users *userO = [[DatabaseSingleton shareDatabaseSingleton] selectUser:self.aField.text];
    
    if ([userO.nameUser isEqualToString:self.aField.text] && [userO.passUser isEqualToString:self.bField.text]) {
        
        
        
        [[NSUserDefaults standardUserDefaults] setObject:userO.nameUser forKey:@"用户名"];
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"1"];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆成功" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self dismissViewControllerAnimated:YES completion:^{
                
                
            }];
            
        }];
        [alert addAction:action];
    }else{
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆失败" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alert addAction:action];

        
    }
    
    
    
    
    
    
}
-(void)button2Action:(UIButton *)button{
    RegisterViewController *registerView = [[RegisterViewController alloc] init];
    
    [self.navigationController pushViewController:registerView animated:YES];
    
    
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.aField resignFirstResponder];
    [self.bField resignFirstResponder];
}

-(void)leftActio:(UIBarButtonItem *)left{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
